from datetime import datetime
from applications.sql_config import db
from werkzeug.security import generate_password_hash, check_password_hash


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True,autoincrement=True)
    userName = db.Column(db.String(80), unique=True, nullable=False)  
    email = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.String(200), nullable=False)  # 增加非空约束
    # 新增time字段（注册时间）
    time = db.Column(db.DateTime, default=datetime.utcnow)  
    # 新增avatar字段（头像URL）
    avatar = db.Column(db.String(200), default=r'C:\Users\admin\Desktop\project\newproject\applications\static\images\default_avatar.png') 
    # 默认头像路径由网站文件夹确定 

    def set_password(self, password: str):
        """生成密码哈希并存储"""
        self.password = generate_password_hash(
            password, 
            method='pbkdf2:sha256', 
            salt_length=16
        )
    
    def check_password(self, password: str) -> bool:
        """验证密码是否匹配哈希值"""
        return check_password_hash(self.password, password)

    
